<template>
  <el-dialog :title="form.id?'修改物料':'新增物料'" :visible.sync="show" width="70%" v-if="show">
    <span class="wf">
      <el-form ref="form" :model="form" label-width="80px">
        <div class="wf x">
          <el-form-item label="id" class="w150px nbx">
            <el-input v-model="form.id" disabled />
          </el-form-item>
          <el-form-item label="隶属分类">
            <!-- <el-input v-model="form.pid" /> -->
            <el-cascader
              :show-all-levels="false"
              :options="itemType"
              :props="{ value:'id',label:'name',checkStrictly: true}"
              v-model="itemPid"
              clearable
            />
          </el-form-item>
          <el-form-item label="物料名称">
            <el-input
              v-model="form.name"
              @focus="$event.currentTarget.select();"
              placeholder="请输入物料分类名称"
            />
          </el-form-item>
        </div>
        <el-form-item label="物料编号">
          <el-input
            v-model="form.uid"
            @focus="$event.currentTarget.select();"
            placeholder="请输入物料编号"
          />
        </el-form-item>
        <el-form-item label="物料型号">
          <el-input
            v-model="form.model"
            @focus="$event.currentTarget.select();"
            placeholder="请输入物料编号"
          />
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="form.other.remarks"
            @focus="$event.currentTarget.select();"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
    </span>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false; ">取 消</el-button>
      <el-button type="primary" @click="show = false; send(form)">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: ["fshow", "form"],
  watch: {
    fshow(a) {
      this.show = true;
    },
    form(a) {
      this.form_(a);
    },
    itemPid(a) {
      if (!this.$gd.itemType) return;
      if (a.length) {
        this.form.pid = a[a.length - 1];
        this.form.cid = this.$gd.itemType.v2item(this.form.pid).cid;
      } else {
        this.form.pid = 0;
        this.form.cid = 0;
      }
    },
    "$gd.itemType"() {
      this.itemType = this.$gd.itemType.arr2tree();
    }
  },
  data() {
    return {
      show: false,
      itemType: [],
      itemPid: []
    };
  },
  created() {
    this.itemType = this.$gd.itemType.arr2tree();
    this.form_(this.form);
  },
  methods: {
    send(v) {
      const data = {
        cmd: v.id ? 1 : 2,
        val: {
          token: sessionStorage.token,
          table: "items"
        }
      };
      let tmp = this.$del(v, "id");
      if (v.id) {
        tmp.where = { id: v.id };
      }
      data.val = Object.assign(data.val, tmp);
      this.$post(data, res => {
        // console.log(this.$cp(res));
        if (res.result) {
          this.$message.success('添加成功');
          this.$emit("change", 201, res.result, !v.id);
        }else this.$message.error(res.error);
      });
    },
    form_(a) {
      if (!a) a = { name: "", other: { remarks: "" } };
      if (!a.other) a.other = { remarks: "" };
      this.itemPid = [];
      if (a.pid) {
        let tmp = this.$gd.itemType.v2item(a.pid);
        while (tmp) {
          this.itemPid.unshift(tmp.id);
          tmp = this.$gd.itemType.v2item(tmp.pid);
        }
      }
    }
  }
};
</script>

